window.Muni = window.Muni || {};
Muni.Polygon = Muni.Polygon || {};

var defaultPolygonOpt = {
    fillColor: "#00000",
    fillOpacity: 0.5,
    strokeColor: "#FF0000",
    strokeOpacity: 0.7,
    strokeWeight: 4,
    path: []
};

var preparePolygonOptions = function(cfg){
    var options = {};
    $.extend(options, defaultPolygonOpt, cfg);
    return options;
};

Muni.Polygon.google = (function(){
    var polygones = [];

    return {
        addPolygon: function(map, cfg){
            var options = preparePolygonOptions(cfg);
            var path = [];
            for(i in options.path){
                var point = new google.maps.LatLng(options.path[i].latitude, options.path[i].longitude);
                path.push(point);
            }
            var gOptions = {
                path: path,
                strokeColor: options.strokeColor,
                strokeOpacity: options.strokeOpacity,
                strokeWeight: options.strokeWeight,
                fillColor: options.fillColor,
                fillOpacity: options.fillOpacity    
            };
            var polygon = new google.maps.Polygon(gOptions);
            polygon.setMap(map);
            polygones.push(polygon);            

            return polygon;
        },

        getPolygones: function(){
            return polygones;
        },

        removePolygon: function(polygon){
            polygon.setMap(null);
            var idx = polygones.indexOf(polygon);
            if(idx == -1){
                polygones.splice(idx, 1);
            }            
        }
        
    };
    
})();

Muni.Polygon.seznam = (function(){
    var layer = null;
    var polygones = [];

    var prepareLayer = function(map){
       if(layer == null){
            layer = new SMap.Layer.Vector();
            map.addLayer(layer);
            layer.enable();
        }
    }

    return {
        addPolygon: function(map, cfg){
            prepareLayer(map);
            var options = preparePolygonOptions(cfg);
            var path = [];
            for(i in options.path){
                var point = SMap.Coords.fromWGS84(options.path[i].latitude, options.path[i].longitude);
                path.push(point);
            };
            var sOptions = {
                color: options.fillColor,
                opacity: options.fillOpacity,
                outlineWidth: options.strokeWeight,
                outlineOpacity: options.strokeOpacity,
                outlineColor: options.strokeColor
            };
            var polygon = new SMap.Geometry(SMap.GEOMETRY_POLYGON, null, path, sOptions);
            layer.addGeometry(polygon);
            polygones.push(polygon);

            return polygon;
        },

        getPolygones: function(){
            return polygones;
        },

        removePolygon: function(polygon){
            layer.removePolygon(polygon);
            var idx = polygones.indexOf(idx);
            if(idx == -1){
                polygones.splice(idx, 1);
            }
        }
    };
})();

Muni.Polygon.bing = (function(){
    var map = null;
    var polygones = [];

    var convertToRGB = function(color){
        var cutHex = (color.charAt(0) == '#') ? color.substring(1, 7) : h;
        var r = parseInt(cutHex.substring(0,2), 16);
        var g = parseInt(cutHex.substring(2, 4), 16);
        var b = parseInt(cutHex.substring(4, 6), 16);
        return [r, g, b];
    };
    
    return {
        addPolygon: function(bmap, cfg){
            map = bmap;
            var options = preparePolygonOptions(cfg);
            var path = [];
            for(i in options.path){
                var point = new Microsoft.Maps.Location(options.path[i].latitude, options.path[i].longitude);
                path.push(point);
            }
            
            var fillRGB = convertToRGB(options.fillColor);
            var fillColor = new Microsoft.Maps.Color(255 * options.fillOpacity, fillRGB[0], fillRGB[1], fillRGB[2]);

            var strokeRGB = convertToRGB(options.strokeColor);
            var strokeColor = new Microsoft.Maps.Color(255 * options.strokeOpacity, strokeRGB[0], strokeRGB[1], strokeRGB[2]);
            
            var bOptions = {
                strokeColor: strokeColor,
                strokeThickness: options.strokeWeight,
                fillColor: fillColor
            };

            var polygon = new Microsoft.Maps.Polygon(path, bOptions);
            

            map.entities.push(polygon);
            polygones.push(polygon);

            return polygon;
            
        },

        getPolygones: function(){
            return polygones;
        },

        removePolygon: function(polygon){
            map.entities.remove(polygon);
            var idx = polygones.indexOf(polygon);
            if(idx != -1){
                polygones.splice(idx, 1);
            }
        }
    };
})();
